package com.applabs.dnd.ui;

import com.applabs.dnd.core.DNDManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class DNDMeMain extends Activity implements OnClickListener {
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId())
		{
		case com.applabs.dnd.R.id.menu_settings:
			Intent intent = new Intent(this, Profiles.class);
			startActivity(intent);
			return true;
		default: 
			return super.onOptionsItemSelected(item);
		}
	}

	ImageButton btDndStatus;
	DNDManager mDndManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.applabs.dnd.R.layout.activity_dndmain);
        mDndManager = DNDManager.getInstance(getApplicationContext());
        addResources();
        new DNDMeEula(this).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(com.applabs.dnd.R.menu.activity_dndmain, menu);
        return true;
    }
    
    public void addResources()
    {
    	btDndStatus = (ImageButton)findViewById(com.applabs.dnd.R.id.btDndService);
    	btDndStatus.setOnClickListener(this);
    	if ( mDndManager.isServiceRunning() )
    	{
    		btDndStatus.setImageResource(com.applabs.dnd.R.drawable.dndon);
    	}
    	else
    	{
    		btDndStatus.setImageResource(com.applabs.dnd.R.drawable.dndoff);
    	}
    }

	@Override
    public void onClick(View arg0) {
	    if ( arg0.getId() == com.applabs.dnd.R.id.btDndService )
	    {
	    	if ( mDndManager.isServiceRunning() )
	    	{
	    		mDndManager.stopService();
	    		btDndStatus.setImageResource(com.applabs.dnd.R.drawable.dndoff);
	    	}
	    	else
	    	{
	    		mDndManager.startService();
	    		btDndStatus.setImageResource(com.applabs.dnd.R.drawable.dndon);
	    	}
	    }
    }
}